package com.hand.wfive.util;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hand.wfive.App;
import com.hand.wfive.R;

/**
 * 仅用于弹窗样式的activity(QMUI由于上下文问题无法正常弹出)
 */
public class ToastUtils {
    private Context context;
    private TextView tipsText;
    private ImageView ivToastIcon;
    private Toast toast = null;

    public static void showSuccessTip(String msg) {
        ToastUtils toastUtils = new ToastUtils(App.getContext());
        toastUtils.initToast();
        toastUtils.setIcon(R.mipmap.icon_toast_success);
        toastUtils.setText(msg);
        toastUtils.show();
    }

    public static void showNormalTip(String msg) {
        ToastUtils toastUtils = new ToastUtils(App.getContext());
        toastUtils.initToast();
        toastUtils.setIcon(R.mipmap.icon_toast_info);
        toastUtils.setText(msg);
        toastUtils.show();
    }

    public static void showErrorTip(String msg) {
        ToastUtils toastUtils = new ToastUtils(App.getContext());
        toastUtils.initToast();
        toastUtils.setIcon(R.mipmap.icon_toast_error);
        toastUtils.setText(msg);
        toastUtils.show();
    }

    public ToastUtils(Context context) {
        this.context = context;
    }

    public void initToast() {
        if (toast == null) {
            toast = new Toast(context);
            View view = LayoutInflater.from(context).inflate(R.layout.view_toast, null, false);
            tipsText = view.findViewById(R.id.tvErrorTips);
            ivToastIcon = view.findViewById(R.id.ivToastIcon);
            toast.setView(view);

            setGravity(Gravity.CENTER);
            setShowTime(Toast.LENGTH_SHORT);
        }
    }


    public void setGravity(int gravity) {
        toast.setGravity(gravity, 0, 0);
    }

    public void setIcon(int drawableId) {
        ivToastIcon.setImageResource(drawableId);
    }

    public void setText(String tips) {
        tipsText.setText(tips);
    }

    public void show() {
        toast.show();
    }

    public void setShowTime(int time) {
        toast.setDuration(time);
    }

    public void setTextColor(int color) {
        tipsText.setTextColor(context.getResources().getColor(color));
    }

    public void setTextSize(float size) {
        tipsText.setTextSize(size);
    }
}
